<template>
  <!-- page: PostDetail -->
  <Modal
    title="删除文章"
    v-bind:visible="modalIsVisible"
    v-on:modal-on-close="modalIsVisible = false"
    v-on:modal-on-confirm="hideAndDelete"
  >
    <p>确定要删除这篇文章吗？</p>
  </Modal>
  <div class = "post-detail-page">
    <article class = "w-75 mx-auto mb-5 pb-3" v-if="currentPost">
      <img
        class = "rounded-lg img-fluid my-4"
        v-bind:src="currentImageUrl"
        alt="currentPost.title"
        v-if="currentImageUrl"
      >
      <h2 class = "mb-4">{{currentPost.title}}</h2>
      <div class = "user-profile-component border-top border-bottom py-3 mb-5 align-items-center row g-0">
        <div class = "col">
          <user-profile
            v-bind:user="currentPost.author"
            v-if="typeof currentPost.author === 'object'"
          ></user-profile>
        </div>
        <span class = "text-muted col text-right font-italic">发表于：{{currentPost.createdAt}}</span>
      </div>
      <div v-html="currentHTML"></div>
      <div class = "btn-group mt-5" v-if="showEditArea">
        <router-link
          class = "btn btn-success"
          type = "button"
          v-bind:to="{ name: 'create', query: { id: currentPost._id } }"
        >编辑</router-link>
        <button
          class = "btn btn-danger ml-4"
          type = "button"
          v-on:click.prevent="modalIsVisible = true"
        >删除</button>
      </div>
    </article>
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, computed, ref } from 'vue'
import { useStore } from 'vuex'
import { useRoute, useRouter } from 'vue-router'
// 导入 组件
import UserProfile from '@/components/UserProfile.vue'
import Modal from '@/components/Modal.vue'
import createMessage from '@/assets/js/createMessage'
// 导入 数据
import { GlobalDataProps, PostProps, ImageProps, UserProps, ResponseType } from '@/assets/js/store'
// 导入 Markdown-it
import MarkdownIt from 'markdown-it'

export default defineComponent({
  // 页面名称
  name: 'PostDetail',
  // 注册组件
  components: {
    UserProfile,
    Modal
  },

  // 组合式api入口
  setup () {
    const store = useStore<GlobalDataProps>()
    const route = useRoute()
    const router = useRouter()
    const currentId = route.params.id
    const md = new MarkdownIt()
    const modalIsVisible = ref(false)
    onMounted(() => {
      store.dispatch('fetchPost', currentId)
    })
    const currentPost = computed<PostProps>(() => store.getters.getCurrentPost(currentId))
    // 文章详情
    const currentHTML = computed(() => {
      const { content, isHTML } = currentPost.value
      if (currentPost.value && content) {
        return isHTML ? content : md.render(content)
      } else {
        return ''
      }
    })
    // 编辑文章
    const showEditArea = computed(() => {
      const { isLogin, _id } = store.state.user
      if (currentPost.value && currentPost.value.author && isLogin) {
        const postAuthor = currentPost.value.author as UserProps
        return postAuthor._id === _id
      } else {
        return false
      }
    })
    // 用户头图
    const currentImageUrl = computed(() => {
      if (currentPost.value && currentPost.value.image) {
        const { image } = currentPost.value
        return (image as ImageProps).url + '?x-oss-process=image/resize,w_850'
      } else {
        return null
      }
    })
    // 删除文章
    const hideAndDelete = () => {
      modalIsVisible.value = false
      // 删除回调
      store.dispatch('deletePost', currentId).then((rawData: ResponseType<PostProps>) => {
        createMessage('删除成功, 2秒后跳转回专栏首页', 'success', 2000)
        setTimeout(() => {
          router.push({ name: 'column', params: { id: rawData.data.column } })
        }, 2000)
      })
    }
    return {
      currentPost,
      currentImageUrl,
      currentHTML,
      showEditArea,
      modalIsVisible,
      hideAndDelete
    }
  }
})
</script>

<style lang = "css" scoped>
.post-detail-page article img {
  max-width: 1000px;
  display: block;
}
.btn-group{
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
